﻿/// <reference path="../../Scripts/commen_js/jquery-2.0.2.min.js" />
/// <reference path="../../Scripts/commen_js/MJ_JSComment.js" />


function getdata() {
    postdata = "{\"type\":\"getaddproduct\"}";
    mj_ajax("../Handler1.ashx", "json", postdata, showdata)

}
function showdata(data) {
    var bodytable = document.getElementById("bodytable");
    if (data.result == "ok") {
        $("#bodytable").empty();
        var tables = data.table;
        for (i = 0; i < tables.length; i++) {
            var label = document.createElement("label");
            label.className = "checkbox-inline";
            var input = document.createElement("input");
            input.type = "radio";
            input.value = $.trim(tables[i].product_id);
            input.name = "producttype";
            var span = document.createElement("span");
            span.innerText = $.trim(tables[i].product_name);
            label.appendChild(input);
            label.appendChild(span);
            bodytable.appendChild(label);
        }

    }
}

function savechild(){
    postdata = "{\"type\":\"savechildimg\"}";
    mj_ajax("../Handler1.ashx", "json", postdata, add_attribute)
}


function add_attribute(data) {

    if (data.result == "ok") {
        var parent_tr = document.getElementById("already");
        var rowsize = document.getElementById('already').getElementsByTagName('tr').length;
        rowsize++;
        var nowcount = rowsize;
        var tr = document.createElement("tr");
        tr.id = "tr" + nowcount;
        var td1 = document.createElement("td");
        td1.id = "typetd" + nowcount.toString();
        td1.setAttribute("contenteditable", "true");
        td1.innerHTML = $.trim($("#typet").val());

        var td2 = document.createElement("td");
        td2.id = "pricetd" + nowcount.toString();
        td2.setAttribute("contenteditable", "true");
        var jjj = $.trim($("#pricet").val());
        td2.innerHTML = $.trim($("#pricet").val());

        var td3 = document.createElement("td");
        td3.id = "remaintd" + nowcount.toString();
        td3.setAttribute("contenteditable", "true");
        td3.innerHTML = $.trim($("#remaint").val());

        var td4 = document.createElement("td");
        td4.innerHTML = data.msg;

        var td5 = document.createElement("td");
        var inputs = document.createElement("a");
        inputs.id = nowcount;
        inputs.onclick = function () { delect_attribute(this.id) };
        inputs.innerHTML = "删除";
        td5.appendChild(inputs);
        td5.id = "deletetd";
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        tr.appendChild(td4);
        tr.appendChild(td5);
        parent_tr.appendChild(tr);
        $("#typet").val("");
        $("#pricet").val("");
        $("#remaint").val("");
        $("#child").hide();
        $("#upload_out2").show();
    }
}
var jitiao = 1;
function addproduct() {
        var productname = $("#pruductname").val().trim();
        var product_describe = $("#product_describe").val().trim();
        var producttype = $('input:radio[name="producttype"]:checked').val();
        if (isUndefinedNullOrEmpty(producttype) || isUndefinedNullOrEmpty(productname)) {
            layer_alert_error("产品名称或产品类型不能为空！");
            return false;
        }
        if (jitiao == 0) {
            alert("不能重复提交！");
            return;
        }
        jitiao = 0;
        var tab = document.getElementById("already");
        var rows = tab.rows;
        var productdata = "";
        for (var i = 0; i < rows.length; i++) {
            try {
                var type = rows[i].cells[0].innerHTML;
                var price = rows[i].cells[1].innerHTML;
                var remian = rows[i].cells[2].innerHTML;
                var img = rows[i].cells[3].innerHTML;
                productdata += "=" + type + "|" + price + "|" + remian + "|" + img
            }
            catch (e) { }
        }
        postdata = "{\"type\":\"addproduct\",\"htmldata\":\"" + productname + "|" + product_describe + "|" + producttype + "\",\"productdata\":\"" + productdata + "\"}";
        mj_ajax("../Handler1.ashx", "json", postdata, showresult)
 
}
function showresult(data) {
    alert(data.msg);
}

function delect_attribute(id) {
    $("#tr" + id).remove();
}




